<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Location extends MX_Controller {

	public function __construct() {
		parent::__construct();

		$this->template
		->set_layout('default') // application/views/layouts/two_col.php
		->set_theme('admin'); // application/views/layouts/two_col.php
		$this->load->model(array('admin_member_model','admin_settings_model','admin_location_model'));
		$this->admin_member_model->check_admin_login();

		$this->template->set_partial('nav_bar', 'layouts/nav_bar');
		$this->template->set_partial('side_menu', 'layouts/side_menu');
		$this->template->set_partial('footer','footer/footer');

		$config = array('table'=>'location');
		$this->load->library('mahana_hierarchy');
		$this->mahana_hierarchy->initialize($config);

	}

	function all($state_id,$loc_id=0)
	{
		if(!$state_id){
			$state_id = 0;
		}else{
			$state_id = $state_id;
		}
		$data['mode'] = "ALL";
		$data['state_id'] = $state_id;
		$data['state'] =  $this->admin_location_model->getStates($state_id=$state_id);

		$this->template->build('location/location',$data);

	}

	function add($state_id){
	
	
	
		if(!$state_id){
			$state_id = 0;
		}else{
			$state_id = $state_id;
		}
	
		$data['mode'] = 'STATE';
		$data['state_id'] = $state_id;
		$this->template->build('location/location',$data);
	
	}
	
	function edit($id){
	
		$data['mode'] = "EDIT";
		$data['location']['info'] = $this->admin_location_model->get($id);
		
		$data['id'] = $id;
		
		$data['loc_id'] = $data['location']['info']->parent_id;
		
		
		//die();
		$this->template->build('location/location',$data);
	}
	
	
	function insert(){
		
			$this->form_validation->set_rules('location','Name','trim|required|xss_clean');
			$this->form_validation->set_rules('seourl','Seo URL','trim|required|xss_clean');
	
			if($this->form_validation->run() == true){
	
				$data=array(
						'parent_id' => $this->input->post('state_id'),
						'name' => $this->input->post('location'),
						'slug' => $this->input->post('seourl'),
						'meta_title' => $this->input->post('meta_title'),
						'meta_keyword' => $this->input->post('meta_keyword'),
						'meta_description' => $this->input->post('meta_description')
	
				);
				
			//	var_dump($data);die();
	
				$this->mahana_hierarchy->insert($data);
				$msg = "Addedd Successfully...";
				$this->session->set_flashdata('message', $msg);
				redirect('secure/location/add/'.$this->input->post('state_id'));
			}
			else{
				//$msz = "Fields Can not be null...";
				//$this->session->set_flashdata('message', $msz);
				$this->add($this->input->post('state_id'));
			}
		}
	
	function update()
	{
			
			$this->form_validation->set_rules('location','Name','trim|required|xss_clean');
			$this->form_validation->set_rules('seourl','Seo URL','trim|required|xss_clean');
	
			if($this->form_validation->run() == true){
	
				$id = $this->input->post('id');
			
						
					$data=array(
							'name' => $this->input->post('location'),
							'slug' => $this->input->post('seourl'),
							'meta_title' => $this->input->post('meta_title'),
							'meta_keyword' => $this->input->post('meta_keyword'),
							'meta_description' => $this->input->post('meta_description')
	
					);

					$this->db->where('id',$id);
					$this->db->update('location',$data);
					$msg = "Updated Successfully...";
					$this->session->set_flashdata('message', $msg);
					redirect('secure/location/edit/'.$id);
				
			}else{//$msz = "Fields Can not be null...";
				//$this->session->set_flashdata('message', $msz);
				$this->edit($id);
			}
		}

		function delete($pkID=0) {
			$this->mahana_hierarchy->delete($pkID,true);
			redirect('secure/location/all/0');
		}
}